package com.mylibraryapplication.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;

import com.mylibrary.util.AbViewUtil;
import com.mylibrary.util.Tools;
import com.mylibrary.view.banner.ConvenientBanner;
import com.mylibrary.view.banner.NetworkImageHolderView;
import com.mylibrary.view.banner.holder.CBViewHolderCreator;
import com.mylibrary.view.banner.listener.OnItemClickListener;
import com.mylibrary.view.tabIndicator.IndicatorViewPager;
import com.mylibrary.view.tabIndicator.ScrollIndicatorView;
import com.mylibrary.view.tabIndicator.slidebar.DrawableBar;
import com.mylibrary.view.tabIndicator.slidebar.ScrollBar;
import com.mylibrary.view.tabIndicator.transition.OnTransitionTextListener;
import com.mylibraryapplication.R;
import com.mylibraryapplication.adapter.TabIndicatorAdapter;
import com.mylibraryapplication.base.BaseActivity;
import com.mylibraryapplication.view.TitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
/**
 * @author yinsujun  Created at 2016/7/27 16:17
 *         头部指示器
 */
public class TabIndicatorActivity extends BaseActivity implements OnItemClickListener{
    @Bind(R.id.title)
    TitleView title;
    @Bind(R.id.convenient_banner)
    ConvenientBanner convenientBanner;
    @Bind(R.id.scroll_indicator)
    ScrollIndicatorView scrollIndicatorView;
    @Bind(R.id.viewpager)
    ViewPager viewPager;
    private String[] bannerUrls={"http://pic28.nipic.com/20130503/10003177_111326031000_2.jpg"
            ,"http://a0.att.hudong.com/15/08/300218769736132194086202411_950.jpg"
            ,"http://i9.qhimg.com/t01b3c35c4247fdc250.jpg"
            ,"http://pic20.nipic.com/20120407/9756907_153426543186_2.jpg"};
    private List<String> localImages=new ArrayList<>();
    private IndicatorViewPager indicatorViewPager;

    @Override
    public int getLayoutId() {
        return R.layout.activity_tab_indicator;
    }

    @Override
    public void initView() {
        scrollIndicatorView= (ScrollIndicatorView) findViewById(R.id.scroll_indicator);
        title= (TitleView) findViewById(R.id.title);
        convenientBanner= (ConvenientBanner) findViewById(R.id.convenient_banner);
        viewPager= (ViewPager) findViewById(R.id.viewpager);
        setTitle(title,"图片轮播Tab指示");
        setData();
    }

    private void setData(){
        initBanner();
        initTab();
    }
    @SuppressWarnings("unchecked")
    private void initBanner() {
        for(int i=0;i<bannerUrls.length;i++){
            localImages.add(bannerUrls[i]);
        }
        convenientBanner.setPages(
                new CBViewHolderCreator<NetworkImageHolderView>() {
                    @Override
                    public NetworkImageHolderView createHolder() {
                        return new NetworkImageHolderView();
                    }
                }, localImages)
                //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                .setPageIndicator(new int[]{R.mipmap.ic_normal_circle, R.mipmap.ic_select_circle})
                //指示器圆点位置，默认居中
//                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT)
                .setOnItemClickListener(this);
//        convenientBanner.setManualPageable(false);//设置不能手动影响

    }

    private void initTab(){
        int selectColor= ContextCompat.getColor(mContext,R.color.app_main_color);
        int normalColor=ContextCompat.getColor(mContext,R.color.defaultTextColor);
        scrollIndicatorView.setOnTransitionListener(new OnTransitionTextListener().setColor(selectColor, normalColor));
        scrollIndicatorView.setScrollBar(new DrawableBar(mContext, R.drawable.green_border_background, ScrollBar.Gravity.CENTENT_BACKGROUND) {
            @Override
            public int getHeight(int tabHeight) {
                return tabHeight - AbViewUtil.dip2px(mContext,12);
            }

            @Override
            public int getWidth(int tabWidth) {
                return tabWidth - AbViewUtil.dip2px(mContext,12);
            }
        });
        indicatorViewPager = new IndicatorViewPager(scrollIndicatorView, viewPager);
        indicatorViewPager.setAdapter(new TabIndicatorAdapter(getSupportFragmentManager(),mContext));
    }



    // 开始自动翻页
    @Override
    public void onResume() {
        super.onResume();
        //开始自动翻页
        if (bannerUrls.length > 1) {
            convenientBanner.startTurning(3000);
        }
    }

    // 停止自动翻页
    @Override
    public void onPause() {
        super.onPause();
        //停止翻页
        convenientBanner.stopTurning();
    }


    @Override
    public void onItemClick(int position) {
        //图片轮播点击事件
        Tools.showTextToast(mContext,"点击了第"+position+"个");
    }
}
